﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Account_UserEdit : BasePage
{
    #region 属性
    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }
    #endregion

    Sys_User model = new Sys_User();
    Sys_UserBLL bll = new Sys_UserBLL();
    Sys_RoleBLL role_bll = new Sys_RoleBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        setUserControlStatus();

        if (!IsPostBack)
        {
            Id = base.ParseQueryInt("Id", 0);
            Action = base.ParseQueryString("Action", "Add");

            BindRepeaterData();

            if (Action == "Modify" || Action == "Detail")
            {
                getDate(Id);
            }
            SetControlStatu();
        }
    }

    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission("001003");
        this.btn_Modify.Enabled = base.CheckUserPermissionOperation("001003", "Add");
        this.btn_Save.Enabled = base.CheckUserPermissionOperation("001003", "Add") || base.CheckUserPermissionOperation("001003", "Edit");
    }
    #endregion

    #region 设置控件状态
    private void SetControlStatu()
    {
        if (Action == "Detail")
        {
            this.txt_UserName.Enabled = false;
            this.txt_Account.Enabled = false;
            this.rbl_Gender.Enabled = false;
            this.txt_Birthday.Enabled = false;
            this.txt_Phone.Enabled = false;
            this.txt_CertificateNo.Enabled = false;
            //this.txt_Post.Enabled = false;
            //this.txt_Department.Enabled = false;

            this.ch_Valid.Enabled = false;
            this.ddl_Role.Enabled = false;

            this.btn_Modify.Visible = true;
            this.btn_Save.Visible = false;

        }
        else if (Action == "Add" || Action == "Modify")
        {
            this.txt_UserName.Enabled = true;
            this.txt_Account.Enabled = true;
            this.rbl_Gender.Enabled = true;
            this.txt_Birthday.Enabled = true;
            this.txt_Phone.Enabled = true;
            this.txt_CertificateNo.Enabled = true;
            //this.txt_Post.Enabled = true;
            //this.txt_Department.Enabled = true;
            this.ddl_Role.Enabled = true;

            this.btn_Modify.Visible = false;
            this.btn_Save.Visible = true;
        }
    }
    #endregion

    #region 绑定数据
    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    private void getDate(int id)
    {
        model = bll.GetModel(id);
        if (model == null)
        {
            base.Alert("获取数据失败！" + id);
            return;
        }
        this.txt_UserName.Text = model.UserName;
        this.txt_Account.Text = model.Account;
        this.rbl_Gender.SelectedValue = model.Gender.ToString();
        this.txt_Birthday.Text = model.Birthday;
        this.txt_Phone.Text = model.Phone;
        this.txt_CertificateNo.Text = model.CertificateNo;
        //this.txt_Post.Text = model.Post;
        //this.txt_Department.Text = model.Department;
        this.ch_Valid.Checked = (model.Valid == 1);

        this.ddl_Role.SelectedValue = model.RoleId.ToString();

        //for (int i = 0; i < rpt_list.Items.Count; i++)
        //{

        //    HiddenField hf_RoleId = (HiddenField)rpt_list.Items[i].FindControl("hf_RoleId");
        //    if (hf_RoleId.Value == model.RoleId.ToString())
        //    {
        //        RadioButton rb_Select = (RadioButton)rpt_list.Items[i].FindControl("rb_Select");
        //        rb_Select.Checked = true;
        //        break;
        //    }
        //}
    }

    private void BindRepeaterData()
    {
        List<Sys_Role> list = role_bll.GetModelList("Valid=1");
        this.ddl_Role.DataTextField = "RoleName";
        this.ddl_Role.DataValueField = "RoleId";
        this.ddl_Role.DataSource = list;
        this.ddl_Role.DataBind();
        this.ddl_Role.Items.Insert(0, new ListItem("----请选择----", "0"));
    }
    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        //检验数据是否重复
        string strCheck = string.Format("(Account like '{0}') and UserId<>{1}",
            TextUtility.ReplaceText(this.txt_Account.Text.Trim()),
            Id.ToString());

        if (bll.GetRecordCount(strCheck) > 0)
        {
            base.Alert("模块名称或模块编码已存在！");
            return;
        }

        if (Action == "Modify")
        {
            model = bll.GetModel(Id);
        }
        else if (Action == "Add")
        {
            model = new Sys_User();
        }
        else
        {
            base.Alert("异常操作！");
            return;
        }

        model.UserName = this.txt_UserName.Text.Trim();
        model.Account = this.txt_Account.Text.Trim();
        model.Gender = Convert.ToInt32(this.rbl_Gender.SelectedValue);
        model.Birthday = this.txt_Birthday.Text.Trim();
        model.Phone = this.txt_Phone.Text.Trim();
        model.CertificateNo = this.txt_CertificateNo.Text.Trim();
        //model.Post = this.txt_Post.Text.Trim();
        //model.Department = this.txt_Department.Text.Trim();
        model.Valid = this.ch_Valid.Checked ? 1 : 0;
        model.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.UpdateUserId = 1;
        model.RoleId = Convert.ToInt32(this.ddl_Role.SelectedValue);
        //for (int i = 0; i < rpt_list.Items.Count; i++)
        //{
        //    RadioButton rb_Select = (RadioButton)rpt_list.Items[i].FindControl("rb_Select");
        //    if (rb_Select.Checked)
        //    {
        //        HiddenField hf_RoleId = (HiddenField)rpt_list.Items[i].FindControl("hf_RoleId");
        //        model.RoleId = Convert.ToInt32(hf_RoleId.Value);
        //        break;
        //    }
        //}

        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);
            }
            else if (Action == "Add")
            {
                //保存
                model.UserPwd = Bangle.Common.MD5.EncryptMD5("123456");
                model.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                model.CreateUserId = 1;
                Id = bll.Add(model);
            }
            base.Redirect("操作成功！", "UserEdit.aspx?Action=Detail&Id=" + Id.ToString());
        }
        catch (Exception ex)
        {
            base.AlertError(ex.ToString());
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserEdit.aspx?Action=Modify&Id=" + Id);
    }
    #endregion


}